#include "ContraState.h"




ContraState::ContraState(ContraData* data)
{
	_data = data;
	_data->state = _DATA_CONTRA_STATE_ON_LAND;
	_stateDead = new ContraStateDead(data);
	_stateInWater = new ContraStateInWater(data);
	_stateOnLand = new ContraStateOnLand(data);
	_stateLandWater = new ContraStateLandWater(data);
	_currentState = _stateOnLand;
}

void ContraState::stand()
{
	_currentState->stand();
}

void ContraState::standUp()
{
	_currentState->standUp();
}

void ContraState::moveRight()
{
	_currentState->moveRight();
}

void ContraState::moveLeft()
{
	_currentState->moveLeft();
}

void ContraState::fire()
{
	_currentState->fire();
}

void ContraState::sit()
{
	_currentState->sit();
}

void ContraState::update()
{
	if (_currentState == _stateDead && !_data->isDead) {
		_currentState = _stateOnLand;
	}
	_currentState->update();
}

void ContraState::draw(long time)
{
	_currentState->draw(time);
}

void ContraState::fly()
{
	_currentState->fly();
}

void ContraState::setState(int state)
{
	if (state == 1) {
		_data->getRect()->Height = 75;
		if (_data->state == _DATA_CONTRA_STATE_IN_WATER) {
			if (_data->bodySprites->state % 2 == 0) 
				_stateLandWater->setDirection(-1, _data->state);
			else 
				_stateLandWater->setDirection(1, _data->state); 
			_currentState = _stateLandWater;
		} else if(_data->state == _DATA_CONTRA_STATE_ON_LAND) {
			_currentState = _stateOnLand;
			_data->isSwimming = false;
			_stateOnLand->firstOnLand();
		}
	} else if (state == 2) {
		_data->getRect()->Height = 40;
		if (_data->state == _DATA_CONTRA_STATE_ON_LAND) {
			if (_data->bodySprites->state % 2 == 0) 
				_stateLandWater->setDirection(-1, _data->state);
			else 
				_stateLandWater->setDirection(1, _data->state); 
			_currentState = _stateLandWater;
		} else if(_data->state == _DATA_CONTRA_STATE_IN_WATER) {
			_data->isSwimming = true;
			_currentState = _stateInWater;
		}
	} else if (state == 3) {
		_data->isDead = true;
		_currentState = _stateDead;
		_data->undeadDelay = 300;
		_data->sprites->currentSprite->Reset();
		_data->bodySprites->currentSprite->Reset();
		_data->rect->Vy = 0;
		_data->rect->Vx = 0.5;
		_data->deady = _data->rect->Y;
		_data->deadx = _data->rect->X;
		_data->rect->Height = 40;
		if (GameDeviceSingleton::getInstance()->getLogic()->playerLife > 0)
			GameDeviceSingleton::getInstance()->getLogic()->playerLife--;
		else 
			GameDeviceSingleton::getInstance()->getLogic()->playerLife = 4;
	}
}
